// 当前模板ID和业务类名
var templateId = null;
var businessClassName = null;
var appId = null;

layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'jquery', 'winui', 'form', 'textool'], function (exports) {
    winui.renderColor();
    var index = parent.layer.getFrameIndex(window.name);
    var $ = layui.$,
        form = layui.form,
        textool = layui.textool;

    // 初始化数据
    templateId = GetUrlParam("id") || null;
    businessClassName = GetUrlParam("businessClassName") || null;
    appId = GetUrlParam("appId") || null;

    if (isNull(templateId)) {
        skyeyeClassEnumUtil.showEnumDataListByClassName("paperSize", 'select', "paperSize", '', form);
        skyeyeClassEnumUtil.showEnumDataListByClassName("orientation", 'select', "orientation", '', form);
        skyeyeClassEnumUtil.showEnumDataListByClassName("commonIsDefault", 'radio', "isDefaultRadio", '', form);
        skyeyeClassEnumUtil.showEnumDataListByClassName("commonEnable", 'radio', "enabledRadio", '', form);

        // 获取表单布局列表
        loadFormLayouts();
    } else {
        AjaxPostUtil.request({
            url: reqBasePath + 'queryPrintTemplateById',
            params: { id: templateId },
            type: 'json',
            method: 'POST',
            callback: function (json) {
                const data = json.bean
                $("#name").val(data.name);
                skyeyeClassEnumUtil.showEnumDataListByClassName("paperSize", 'select', "paperSize", data.paperSize, form);
                skyeyeClassEnumUtil.showEnumDataListByClassName("orientation", 'select', "orientation", data.orientation, form);
                skyeyeClassEnumUtil.showEnumDataListByClassName("commonIsDefault", 'radio', "isDefaultRadio", data.isDefault, form);
                skyeyeClassEnumUtil.showEnumDataListByClassName("commonEnable", 'radio', "enabledRadio", data.enabled, form);

                // 检查是否自定义纸张大小
                if (data.paperSize === 'CUSTOM_PAGE') {
                    $("#customSizeItem").show();
                }

                // 获取表单布局列表
                loadFormLayouts(data.pageId);
            },
            async: false
        });
    }
    textool.init({ eleId: 'remark', maxlength: 200 });
    form.render();

    // 加载表单布局列表
    function loadFormLayouts(pageId) {
        AjaxPostUtil.request({
            url: reqBasePath + 'queryDsFormPageList',
            params: {
                className: businessClassName,
                appId: appId
            },
            type: 'json',
            callback: function (json) {
                var layouts = json.rows || [];
                var $select = $("select[name='pageId']");
                $select.empty().append('<option value="">请选择表单布局</option>');

                $.each(layouts, function (index, item) {
                    var option = $('<option></option>').val(item.id).text(item.name + '（' + item.numCode + '）');
                    $select.append(option);
                });
                if (!isNull(pageId)) {
                    $select.val(pageId);
                }

                form.render('select');

            }
        });
    }

    // 监听纸张大小选择
    form.on('select(paperSize)', function (data) {
        if (data.value === 'CUSTOM_PAGE') {
            $("#customSizeItem").show();
        } else {
            $("#customSizeItem").hide();
        }
    });

    matchingLanguage();
    form.render();
    // 表单提交
    form.on('submit(formSubmit)', function (data) {
        if (winui.verifyForm(data.elem)) {
            // 验证自定义纸张大小
            if (dataShowType.getData('paperSize') === 'CUSTOM_PAGE') {
                if (!$("#width").val() || !$("#height").val()) {
                    winui.window.msg('请输入自定义纸张的宽度和高度', { icon: 2, time: 2000 });
                    return false;
                }
            }

            var params = {
                id: templateId || null,
                name: $("#name").val(),
                pageId: $("#pageId").val(),
                paperSize: dataShowType.getData('paperSize'),
                width: $("#width").val(),
                height: $("#height").val(),
                orientation: dataShowType.getData('orientation'),
                isDefault: dataShowType.getData('isDefaultRadio'),
                enabled: dataShowType.getData('enabledRadio'),
                remark: $("#remark").val(),
                objectKey: businessClassName
            }

            // 提交数据
            AjaxPostUtil.request({
                url: reqBasePath + 'writePrintTemplate',
                params: params,
                type: 'json',
                method: 'POST',
                callback: function (json) {
                    parent.layer.close(index);
                    parent.refreshCode = '0';
                }
            });
        }

        return false;
    });

    $("body").on("click", "#cancle", function () {
        parent.layer.close(index);
    });
});